package com.mycompany;

import java.util.ArrayList;

import org.apache.wicket.RequestCycle;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBoxMultipleChoice;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

public class DropDownWithCheckBoxes extends Panel {

	static final long serialVersionUID = Math.round((Math.random()*100000));

	private ArrayList<String> MANY_CHOICES = new ArrayList<String>();
	ArrayList<String> aList = new ArrayList<String>();
	
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public DropDownWithCheckBoxes(String id, Model<String> headermodel, Model<String> imagemodel, final Label toBeUpdated, ArrayList al) {
		super(id);

        PropertyModel messageModel = new PropertyModel(this, "aList");
        PropertyModel messageModel2 = new PropertyModel(this, "aList");
		Label header = new Label("headline",headermodel);
		add(header);
		Image image = new Image("arrow",imagemodel);
		add(image);

		CheckBoxMultipleChoice cbmChoice = new CheckBoxMultipleChoice("choices", MANY_CHOICES);
		cbmChoice.setModel(messageModel);
		add(cbmChoice);
		final CheckBoxMultipleChoice cbmChoice2 = new CheckBoxMultipleChoice("choices2", MANY_CHOICES);
		cbmChoice2.setModel(messageModel2);
		add(cbmChoice2);
		
		final AbstractDefaultAjaxBehavior behave = new AbstractDefaultAjaxBehavior() {
		    /**
			 * 
			 */
			private static final long serialVersionUID = 4234852329983895995L;

			protected void respond(final AjaxRequestTarget target) {
		    	String paramFoo = RequestCycle.get().getRequest().getParameter("title");
		    	paramFoo = getNamesForIndex(paramFoo);
		    	// System.out.println("value: "+paramFoo);
		    	toBeUpdated.setDefaultModel(new Model<String>(paramFoo));
		    	toBeUpdated.modelChanged();
		    	target.addComponent(toBeUpdated);
		    }
		};
		add(behave);
		
		Label myScript = new Label("myScript", new JSReadOnlyModel("function callWicket(args) { var wcall=wicketAjaxPost('"+behave.getCallbackUrl()+"'+args,function() { }, function() { });}"));
		myScript.setEscapeModelStrings(false); // do not HTML escape JavaScript code
		add(myScript);
	}
	
	public String getNamesForIndex(String paramFoo) {
		StringBuffer temp = new StringBuffer();
		String sDefault = "Assign Team";
		if (paramFoo == null) return sDefault;
		if (paramFoo.equals("")) return sDefault;
		String[] indexArray = paramFoo.split("\\|");
		for (int i=0;i<indexArray.length;i++) {
			int j = (int) Integer.parseInt(indexArray[i]);
			temp = (StringBuffer) temp.append(this.MANY_CHOICES.get(j));
			temp.append("|");
		}
		return temp.substring(0, temp.length()-1);
	}
}
